package Web;

import Bean.Page;
import Utils.WebUtils;

import service.BookService;
import service.BookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class bookStoreServlet extends BaseServlet {

    BookService bookService = new BookServiceImpl();

    protected void pageByPrice(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1. 获取请求参数pageNo
        int pageNo = WebUtils.parseInt(request.getParameter("pageNo"),1);
        int minPrice = WebUtils.parseInt(request.getParameter("minPrice"),0);
        int maxPrice = WebUtils.parseInt(request.getParameter("maxPrice"),Integer.MAX_VALUE);
        //2. 调用 BookService.page(pageNo,pageSize, minPrice, maxPrice):page对象
        Page page = bookService.pageByPreice(pageNo, 4, minPrice, maxPrice);
        StringBuilder sb = new StringBuilder("client/bookStoreServlet?action=pageByPrice");
        if (request.getParameter("minPrice") != null){
            sb.append("&minPrice=").append(request.getParameter("minPrice"));
        }
        if (request.getParameter("maxPrice") != null){
            sb.append("&maxPrice=").append(request.getParameter("maxPrice"));
        }
        page.setUrl(sb.toString());
        //3.保存 page 对象到 Request 域中
        request.setAttribute("page", page);
        request.getRequestDispatcher("/pages/client/index.jsp").forward(request, response);
    }

    protected void page(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1. 获取请求参数pageNo
        int pageNo = WebUtils.parseInt(request.getParameter("pageNo"),1);
        //2. 调用 BookService.page(pageNo,pageSize):page对象
        Page page = bookService.page(pageNo, 4);
        page.setUrl("client/bookStoreServlet?action=page");
        //3.保存 page 对象到 Request 域中
        request.setAttribute("page", page);
        request.getRequestDispatcher("/pages/client/index.jsp").forward(request, response);
    }
}
